//
//  BLAddFriendViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/16.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLAddFriendViewController.h"
#import "BLNetwork.h"
#import "MJExtension.h"
#import "BLLeftTableViewCell.h"
#import "BLRightTableViewCell.h"
#import "BLClassify.h"
#import "BLPerson.h"
#import "SVProgressHUD.h"
#import "MJRefresh.h"

#define leftSelectRow self.leftData[[self.leftTableView indexPathForSelectedRow].row]

@interface BLAddFriendViewController ()<UITableViewDataSource,UITableViewDelegate,BLRightTableViewCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@property (weak, nonatomic) IBOutlet UITableView *leftTableView;

@property (nonatomic, strong)NSArray *leftData;

@property (nonatomic, strong)NSArray *rightData;

@property (nonatomic, strong)BLClassify *classify;
@end

@implementation BLAddFriendViewController

- (NSArray *)leftData
{
    if (!_leftData) {
        _leftData = [NSArray array];
    }
    return _leftData;
}

- (NSArray *)rightData
{
    if (!_rightData) {
        _rightData = [NSArray array];
    }
    return _rightData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BLBg;
    self.title = @"推荐关注";
    [SVProgressHUD show] ;
    
    [self setupLeftTable];
    [self getLeftData];
    [self setupRightTable];
    
    
}

- (void)setupLeftTable
{
    [self.leftTableView registerNib:[UINib nibWithNibName:@"BLLeftTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"leftCell"];
    self.leftTableView.backgroundColor = BLColor(240, 240, 240);
    
}


- (void)setupRightTable
{
    [self.rightTableView registerNib:[UINib nibWithNibName:@"BLRightTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"rightCell"];
    self.rightTableView.backgroundColor = [UIColor whiteColor];
    self.rightTableView.rowHeight = 60;
    self.rightTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.rightTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    self.rightTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reflash)];
   
}

- (void)getLeftData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"category";
    params[@"c"] = @"subscribe";
    
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        NSArray *tempArray = result[@"list"];
        self.leftData = [BLClassify mj_objectArrayWithKeyValuesArray:tempArray];
        [self.leftTableView reloadData];
        [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
//        [self getRightDataWithCategoryId:classify.id Classify:classify];
        [self getRightData];
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载出错！"];
    }];
}


- (void)getRightDataWithCategoryId:(NSString *)categoryId Classify:(BLClassify *)classify
{
    self.classify = leftSelectRow;
    if (classify.list.count) {
        self.rightTableView.mj_footer.hidden = YES;
        self.rightData = classify.list;
        [self.rightTableView reloadData];
        [self.rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        self.rightTableView.mj_footer.hidden = NO;
    }else{
        [self.rightTableView.mj_header beginRefreshing];
    }
    
    
}

- (void)getRightData
{
    self.classify = leftSelectRow;
    BLClassify *classify = self.classify;
    self.rightTableView.mj_footer.hidden = YES;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"subscribe";
    params[@"category_id"] = self.classify.id;
    
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        NSArray *tempArray = result[@"list"];
        classify.list = [BLPerson mj_objectArrayWithKeyValuesArray:tempArray];
        classify.total = result[@"total"];
        classify.curentPage = 1;
        self.rightData = classify.list;
        [self.rightTableView reloadData];
        [SVProgressHUD dismiss];
        [self.rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        [self.rightTableView.mj_header endRefreshing];
        self.rightTableView.mj_footer.hidden = NO;
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载出错！"];
    }];

}

- (void)loadMore
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"subscribe";
    params[@"category_id"] = self.classify.id;
    int page = ++self.classify.curentPage ;
    params[@"page"] = [NSString stringWithFormat:@"%d",page];
    
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        NSArray *tempArray = result[@"list"];
        tempArray = [BLPerson mj_objectArrayWithKeyValuesArray:tempArray];
        [self.classify.list addObjectsFromArray:tempArray];
        [self.rightTableView reloadData];
        if (self.classify.list.count == [self.classify.total intValue]) {
            [self.rightTableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.rightTableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载出错！"];
    }];

}

- (void)reflash
{
    if (self.classify.list) {
        self.classify.list = nil ;
    }
    [self getRightData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) {
        return self.leftData.count;
    }
   
    return  self.rightData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        BLLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];
    cell.classify = self.leftData[indexPath.row];
    return cell;
    }
   
    BLRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];
    cell.person = self.rightData[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        BLClassify *classify = self.leftData[indexPath.row];
        NSString *categoryId = classify.id;
        [self getRightDataWithCategoryId:categoryId Classify:classify];
        
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
  
}



- (void)rightTableViewWith:(UITableViewCell *)cell destController:(UIViewController *)desController
{
    [self presentViewController:desController animated:YES completion:nil];
}

@end
